package xin.clips.system.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

import cn.hutool.core.util.ObjectUtil;
import xin.clips.common.core.domain.AjaxResult;
import xin.clips.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import xin.clips.common.utils.DictUtils;
import xin.clips.common.utils.ShiroUtils;
import xin.clips.common.utils.StringUtils;
import xin.clips.system.domain.gy.GyUserShareDoc;
import xin.clips.system.mapper.gy.GyUserDocMapper;
import xin.clips.system.domain.gy.GyUserDoc;
import xin.clips.system.service.gy.IGyUserDocService;
import xin.clips.common.core.text.Convert;

/**
 * 用户文档Service业务层处理
 *
 * @author clips
 * @date 2023-02-28
 */
@Service
public class GyUserDocServiceImpl implements IGyUserDocService {
    @Autowired
    private GyUserDocMapper gyUserDocMapper;

    /**
     * 查询用户文档
     *
     * @param fileId 用户文档主键
     * @return 用户文档
     */
    @Override
    public GyUserDoc selectGyUserDocByFileId(String fileId) {
        return gyUserDocMapper.selectGyUserDocByFileId(fileId);
    }

    /**
     * 查询用户文档列表
     *
     * @param gyUserDoc 用户文档
     * @return 用户文档
     */
    @Override
    public List<GyUserDoc> selectGyUserDocList(GyUserDoc gyUserDoc) {
        return gyUserDocMapper.selectGyUserDocList(gyUserDoc);
    }

    /**
     * 新增用户文档
     *
     * @param gyUserDoc 用户文档
     * @return 结果
     */
    @Override
    public AjaxResult insertGyUserDoc(GyUserDoc gyUserDoc) {

        if (gyUserDoc.getFileType().toString().equals("1")) {
            int index = gyUserDoc.getFileName().lastIndexOf('.');
            gyUserDoc.setFileExt(gyUserDoc.getFileName().substring(index + 1));
            gyUserDoc.setFileName(gyUserDoc.getFileName().substring(0, index));
        }
        gyUserDoc.setUserId(ShiroUtils.getUserId());
        List<GyUserDoc> list = gyUserDocMapper.selectGyUserDocList(gyUserDoc);
        if (ObjectUtil.isNotEmpty(list)) {
            return AjaxResult.error("文件名称不能重复!");
        }
        String uuid = UUID.randomUUID().toString().replace("-", "");
        if (gyUserDoc.getFileType() == 1) {

            if ("xls, xlt, et, , xlsx, xltx, csv, xlsm, xltm".indexOf(gyUserDoc.getFileExt()) > -1)
                gyUserDoc.setFileExt("xls");  // xls, xlt, et, , xlsx, xltx, csv, xlsm, xltm
            if ("doc, dot, wps, wpt, docx, dotx, docm, dotm".indexOf(gyUserDoc.getFileExt()) > -1)
                gyUserDoc.setFileExt("doc");
            if ("ppt,pptx,pptm,ppsx,ppsm,pps,potx,potm,dpt,dps".indexOf(gyUserDoc.getFileExt()) > -1)
                gyUserDoc.setFileExt("ppt");
            if ("txt,TXT".indexOf(gyUserDoc.getFileExt()) > -1)
                gyUserDoc.setFileExt("txt");
            if (gyUserDoc.getFileExt().equals("zip")) gyUserDoc.setFileExt("zip");
            if ("bmp,png,tif,gif,jpg,JPEG,jpeg".indexOf(gyUserDoc.getFileExt()) > -1)
                gyUserDoc.setFileExt("img");

            if (!gyUserDoc.getFileExt().equals("xls") && !gyUserDoc.getFileExt().equals("doc") && !gyUserDoc.getFileExt().equals("ppt")
                    && !gyUserDoc.getFileExt().equals("txt") && !gyUserDoc.getFileExt().equals("zip") && !gyUserDoc.getFileExt().equals("img") && !gyUserDoc.getFileExt().equals("pdf")) {
                gyUserDoc.setFileExt("other");
            }

            GyUserDoc parentUserDoc = gyUserDocMapper.selectGyUserDocByFileId(gyUserDoc.getParentId());
            if(gyUserDoc.getParentId().equals("0")){
                gyUserDoc.setAncestors(uuid);
            }else{
                gyUserDoc.setAncestors(parentUserDoc.getAncestors() + "," + uuid);
            }

        } else {
            if (gyUserDoc.getParentId().equals("0")) {
                gyUserDoc.setParentId("0");
                gyUserDoc.setAncestors(uuid);
            } else {
                GyUserDoc parentUserDoc = gyUserDocMapper.selectGyUserDocByFileId(gyUserDoc.getParentId());
                gyUserDoc.setAncestors(parentUserDoc.getAncestors() + "," + uuid);
                gyUserDoc.setParentId(parentUserDoc.getFileId());
            }


            gyUserDoc.setFileType(0);
        }

        gyUserDoc.setCreateTime(new Date());
        gyUserDoc.setFileId(uuid);
        gyUserDocMapper.insertGyUserDoc(gyUserDoc);
        return AjaxResult.success();
    }


    /**
     * 修改用户文档
     *
     * @param gyUserDoc 用户文档
     * @return 结果
     */
    @Override
    public int updateGyUserDoc(GyUserDoc gyUserDoc) {
        gyUserDoc.setUpdateTime(DateUtils.getNowDate());
        return gyUserDocMapper.updateGyUserDoc(gyUserDoc);
    }

    /**
     * 批量删除用户文档
     *
     * @param fileIds 需要删除的用户文档主键
     * @return 结果
     */
    @Override
    public int deleteGyUserDocByFileIds(String fileIds) {
        return gyUserDocMapper.deleteGyUserDocByFileIds(Convert.toStrArray(fileIds));
    }

    /**
     * 删除用户文档信息
     *
     * @param fileId 用户文档主键
     * @return 结果
     */
    @Override
    public int deleteGyUserDocByFileId(String fileId) {
        return gyUserDocMapper.deleteGyUserDocByFileId(fileId);
    }


    /**
     * 查询用户文档
     *
     * @param parentId 父级文档id
     * @return 用户文档
     */
    @Override
    public GyUserDoc selectGyUserDocByParentId(String parentId) {

        return gyUserDocMapper.selectGyUserDocByParentId(parentId);
    }

    /**
     * 根据ancestors批量获取UserDoc
     *
     * @param fileIds 需要删除的数据主键集合
     * @return 结果
     */
    public List<GyUserDoc> selectGyUserDocByFileIds(String fileIds) {
        List<GyUserDoc> list = new ArrayList<>();
        String[] fidStr = Convert.toStrArray(fileIds);
        for (int i = 0; i < fidStr.length; i++) {
            list.add(this.selectGyUserDocByFileId(fidStr[i]));
        }
        return list;
    }
}
